/*
 *
 *
 */

#include "window_opcode.h"

/* WINDOW_OPCODE_MAJOR_COMMON */
static void window_opcode_action_common(uint32_t minor,
				uint32_t data)
{
	gkt_window_s *window;

	if (WINDOW_OPCODE_COMMON_SYSTEM_INFO == minor)
		gkt_window_show(WINDOW_ID_sysinfo, 0);
	else if (WINDOW_OPCODE_COMMON_CONFIRM == minor) {
		window = gkt_window_find(GKT_WINDOW_ID_ACTIVE);
		if (window) {
			gkt_window_custom_event_handle(window,
					GKT_EVENT_KEY, GKT_KEY_OK, 0, 0);
		}
	}
	else if (WINDOW_OPCODE_COMMON_CANCEL == minor) {
		window = gkt_window_find(GKT_WINDOW_ID_ACTIVE);
		if (window) {
			gkt_window_custom_event_handle(window,
					GKT_EVENT_KEY, GKT_KEY_CANCEL, 0, 0);
		}
	}
	else if (WINDOW_OPCODE_COMMON_EXIT == minor)
		gkt_window_show(GKT_WINDOW_ID_START, 0);
	else if (WINDOW_OPCODE_COMMON_STANDBY == minor)
		gkt_window_system_standby(1);
	else if (WINDOW_OPCODE_COMMON_FACTORY_RESET == minor)
		gkt_window_show(WINDOW_ID_factory_reset, 0);
	else if (WINDOW_OPCODE_COMMON_FACTORY_TEST == minor) {

	}
	else if (WINDOW_OPCODE_COMMON_AGING_TEST == minor) {

	}
	else {
		// TODO: others
	}
}
static uint32_t window_opcode_flags_common(uint32_t minor)
{
	uint32_t flags;

	if (WINDOW_OPCODE_COMMON_SYSTEM_INFO == minor)
		flags = 0;
	else if ((WINDOW_OPCODE_COMMON_CONFIRM == minor)
		|| (WINDOW_OPCODE_COMMON_CANCEL == minor)
		|| (WINDOW_OPCODE_COMMON_EXIT == minor)
		|| (WINDOW_OPCODE_COMMON_STANDBY == minor))
	{
		flags = WINDOW_OPCODE_F_UNLIMITED;
	}
	else if ((WINDOW_OPCODE_COMMON_FACTORY_RESET == minor))
	{
		flags = WINDOW_OPCODE_F_NEED_LOGIN | WINDOW_OPCODE_F_UNLIMITED;
	}
	else if ((WINDOW_OPCODE_COMMON_FACTORY_TEST == minor)
		|| (WINDOW_OPCODE_COMMON_AGING_TEST == minor))
	{
		flags = WINDOW_OPCODE_F_UNINITIALIZED | WINDOW_OPCODE_F_FROM_START;
	}
	else {
		flags = WINDOW_OPCODE_F_INITIALIZED | WINDOW_OPCODE_F_NEED_LOGIN
					| WINDOW_OPCODE_F_FROM_START;
	}

	return flags;
}


/* WINDOW_OPCODE_MAJOR_USER_ADD */
static void window_opcode_action_user_add(uint32_t minor,
				uint32_t data)
{
	window_user_add_data_s add_data;

	gkt_unused(data);

	add_data.authority = (minor & WINDOW_OPCODE_USER_ADD_AUTHORITY_MASK) 
							>> WINDOW_OPCODE_USER_ADD_AUTHORITY_SHIFT;
	add_data.type = minor & WINDOW_OPCODE_USER_ADD_TYPE_MASK;
	add_data.user_index = -1;

	gkt_info("opcode: <USER_ADD> authority(%u), type(%u)\n",
			add_data.authority, add_data.type);
	gkt_window_show(WINDOW_ID_user_add, (uint32_t)&add_data);
}
#define window_opcode_flags_user_add	NULL


/* WINDOW_OPCODE_MAJOR_USER_DELETE */
static void window_opcode_action_user_delete(uint32_t minor,
				uint32_t data)
{

}
#define window_opcode_flags_user_delete	NULL


/* WINDOW_OPCODE_MAJOR_SET */
static const gkt_window_id_t sc_window_opcode_set_window_ids[WINDOW_OPCODE_SET_MAX] = {
	WINDOW_ID_system_time_setting,
	WINDOW_ID_lock_auto_close_time,
#if (GKT_USER_GUEST_MAXNUMS > 0)
	WINDOW_ID_guest_unlock_time_setting
#else
	0
#endif
};
static void window_opcode_action_set(uint32_t minor,
				uint32_t data)
{
	gkt_window_id_t window_id;

	gkt_info("opcode: <SET> sub_type(%u), data(%p)\n", minor, data);
	if (minor < WINDOW_OPCODE_SET_MAX) {
		window_id = sc_window_opcode_set_window_ids[minor];
		if ((window_id > GKT_WINDOW_ID_USER_BASE)
			&& (window_id < WINDOW_ID_MAX))
		{
			gkt_window_show(window_id, data);
		}
	}
}
#define window_opcode_flags_set	NULL


/* switch */
static const gkt_window_id_t sc_window_opcode_switch_window_ids[WINDOW_OPCODE_SWITCH_SUB_TYPE_MAX] = {
#if defined(GKT_CONFIG_WINDOW_DUAL_VERIFY_ENABLE) && GKT_CONFIG_WINDOW_DUAL_VERIFY_ENABLE
	WINDOW_ID_dual_verify,
#else
	WINDOW_ID_MAX,
#endif
#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) && GKT_CONFIG_DOOR_TAMPER_ENABLE
	WINDOW_ID_door_tamper_alarm
#else
	WINDOW_ID_MAX
#endif
};
static void window_opcode_action_switch(uint32_t minor,
				uint32_t data)
{
	uint32_t sub_type, action;
	gkt_window_id_t window_id;

	gkt_unused(data);

	sub_type = (minor >> WINDOW_OPCODE_SWITCH_SUB_TYPE_SHIFT) 
					& WINDOW_OPCODE_SWITCH_SUB_TYPE_MASK;
	action = minor & WINDOW_OPCODE_SWITCH_ACTION_MASK;
	gkt_info("opcode: <SWITCH> sub_type(%u), action(%u)\n",
					sub_type, action);

	if (sub_type < WINDOW_OPCODE_SWITCH_SUB_TYPE_MAX) {
		window_id = sc_window_opcode_switch_window_ids[sub_type];
		if ((window_id > GKT_WINDOW_ID_USER_BASE)
			&& (window_id < WINDOW_ID_MAX))
		{
			gkt_window_show(window_id, action);
		}
	}
}
#define window_opcode_flags_set	NULL

